<%--
  Created by IntelliJ IDEA.
  User: 王辉
  Date: 2022/5/10
  Time: 0:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.lang.*" %>
<% request.setCharacterEncoding("UTF-8");%>
<html>
<head>
    <title>注册中...</title>
</head>
<body>
<%
    String driverName="com.mysql.cj.jdbc.Driver";
    String username = "root";
    String password = "123456";
    String url = "jdbc:mysql://localhost:3306/t_card?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false";
    Class.forName(driverName);
    Connection conn = DriverManager.getConnection(url,username,password);
    String sql = "insert into user(username,password) values(?,?)";
    String sql2 = "select * from user where username = ?";
    PreparedStatement pstmt2 = conn.prepareStatement(sql2);
    String rUsername = request.getParameter("username");
    String rPassword = request.getParameter("password");
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setString(1,rUsername);
    pstmt.setString(2,rPassword);
    pstmt2.setString(1,rUsername);
    ResultSet rs = pstmt2.executeQuery();
    rs.next();
    if(rs.getRow()==0){
    int result = pstmt.executeUpdate();
    if(result==1){
%>
<div align="center">
    <h1>注册成功</h1><br>
    <a href="login.jsp" style="color: chartreuse;">这就去登录</a>
</div>
<%
    }
    else {%>
<div align="center">
    <h1 align="center">注册失败</h1><br>
    <a href="register.jsp" style="color: chartreuse;" align="cencer">重新注册</a>
</div>

<%
    }
    }else{
%>
<div align="center">
    <h1>用户名重复</h1><br>
    请<a href="register.jsp" style="color: red">重试</a>
</div>
<%
    }
%>
</body>
</html>
